package random

import (
	"math/rand"
	"strings"
	"time"
)

const (
	Numbers      = "0123456789"
	AlphaNumbers = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
)

// 初始化随机数生成器
func init() {
	rand.NewSource(time.Now().UnixNano())
}

// GenerateNumericCode 生成纯数字验证码
func GenerateNumericCode(length int) string {
	return generateCode(length, Numbers)
}

// GenerateAlphaNumericCode 生成数字和字母混合的验证码
func GenerateAlphaNumericCode(length int) string {
	return generateCode(length, AlphaNumbers)
}

// generateCode 是生成验证码的通用函数
func generateCode(length int, charset string) string {
	code := make([]string, length)
	charsetLength := len(charset)
	for i := 0; i < length; i++ {
		code[i] = string(charset[rand.Intn(charsetLength)])
	}
	return strings.Join(code, "")
}
